<template>
  <div class="login">
    <h1>欢迎登陆</h1>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户名：" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>

        <el-form-item label="密码：" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>

          <el-form-item label="验证码：" prop="code">
            <img :src="ruleForm.captcha"  alt="验证码" @click="changeCaptcha">
            <el-input v-model="ruleForm.code"></el-input>
          </el-form-item>

      <el-form-item>
        <el-button type="success" @click="login('ruleForm')">登陆</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
//Qs是axios内置的一个模块
import Qs from 'qs'

export default {
  name: 'Login',
  data() {
    return {
      ruleForm: {
        name: '',
       password: '',
       code: '',
        captcha: 'http://localhost:9090/captcha'
      },
      rules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ]
      },
    };
  },

  methods: {
    // 点击图片时更新验证码图片
    changeCaptcha: function () {
      this.ruleForm.captcha = 'http://localhost:9090/captcha?v='+ Date.now();
    },

    login(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        let vm = this;
        if (valid) {
          // 发送axios请求
          this.axios({
            method:"post",
            url:"http://localhost:9090/loginJson",
            data:{
              name:vm.ruleForm.name,
              password: vm.ruleForm.password,
              code:vm.ruleForm.code
            },
            // 需要在这里执行参数的转换
            // axios 默认是使用json格式传递参数的 application/json
            // 后端方法形参上如果没有加@ResquestBody注解 默认是使用www-form-data普通表单接收参数的
            transformRequest: [function (data) {
              return Qs.stringify(data)
            }],
          }).then(function (res){
            console.log(res.data);
            if (res.data.code==0){
                vm.$message({
                  message: res.data.msg,
                  type: 'success'
                });
              //把用户信息放入store中，就可以在其他组件中获取用户信息
              vm.$store.commit('updateUser', res.data.data);
              vm.$router.push("home");
            }else {
              vm.$message({
                message:res.data.msg,
                type:"error"
              });
            }
          });
        } else {
          //校验失败
          vm.$message.error('输入格式错误');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
